package cn.shyouchi.wuguanjia.android.ui.event.temporary;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/17 at 10:10
 * Email: 472279981@qq.com
 * Des:
 */

public class EventTemporaryPresenter implements TemporaryContract.Presenter {
    private TemporaryContract.View mView;
    private Context mContext;
    private EventApi mEventApi;

    public EventTemporaryPresenter(Context context, TemporaryContract.View view) {
        this.mContext = context;
        this.mView = view;
        mEventApi = new EventApi(mContext);

    }

    @Override
    public void getTemporaryList(String projectId) {
        mEventApi.getTemporaryList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<TemporaryEntity, List<TemporaryEntity.ResultMessageBean>>() {
                    @Override
                    public List<TemporaryEntity.ResultMessageBean> call(TemporaryEntity temporaryEntity) {
                        return temporaryEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<TemporaryEntity.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<TemporaryEntity.ResultMessageBean> resultMessageBeanList) {
                        mView.onTemporaryRefresh(resultMessageBeanList);
                    }
                });
    }

    @Override
    public void delTemporary(final String projectId, String id) {
        mEventApi.delTemporary(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        getTemporaryList(projectId);
                    }
                });
    }

    @Override
    public void addTemporary(String projectId, String id) {

    }

    @Override
    public void attachView(@NonNull TemporaryContract.View view) {

    }

    @Override
    public void detachView() {

    }
}
